package app

import (
	"github.com/astaxie/beego/cache"
	_ "github.com/astaxie/beego/cache/redis"
)

var CacheHandler cache.Cache

func InitCache() {
	host := "127.0.0.1:6379"
	passWord := ""
	dbNum := "0"
	if NacosConfig.String("cache::redis_host") != "" {
		host = NacosConfig.String("cache::redis_host")
	}
	if NacosConfig.String("cache::redis_password") != "" {
		passWord = NacosConfig.String("cache::redis_password")
	}
	if NacosConfig.String("cache::redis_dbnum") != "" {
		dbNum = NacosConfig.String("cache::redis_dbnum")
	}
	var err error
	defer func() {
		if r := recover(); r != nil {
			CacheHandler = nil
		}
	}()
	CacheHandler, err = cache.NewCache("redis", `{"conn":"`+host+`","password":"`+passWord+`","dbNum":"`+dbNum+`"}`)
	//cc, err = cache.NewCache("redis", `{"conn":"`+host+`"}`)
	if err != nil {
		println("cache error:", err.Error())
	}
}
